/*********************************************************************************
* Copyright 2019 Huawei Technologies Co.,Ltd.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License.  You may obtain a copy of the
* License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations under the License.
**********************************************************************************
*/
#include "bucket.h"
#include "request_util.h"
#include <openssl/md5.h> 

obs_status get_bucket_version_xml_callback(const char *element_path, const char *data,
    int data_len, void *callback_data)
{
    int fit = 1;

    get_bucket_common_data *version_data = (get_bucket_common_data *)callback_data;
    if (data)
    {
        if (!strcmp(element_path, "VersioningConfiguration/Status")) {
            string_buffer_append(version_data->common_data, data, data_len, fit);
        }
    }

    //(void) fit;
    if (!fit) {
        COMMLOG(OBS_LOGDEBUG, "%s: fit is 0.", __FUNCTION__);
    }
    return OBS_STATUS_OK;
}

obs_status get_bucket_version_properties_callback(const obs_response_properties *response_properties,
    void *callback_data)
{
    get_bucket_common_data *version_data = (get_bucket_common_data *)callback_data;
    if (version_data->responsePropertiesCallback)
    {
        return (*(version_data->responsePropertiesCallback))
            (response_properties, version_data->callback_data);
    }
    return OBS_STATUS_OK;
}

obs_status get_bucket_version_data_callback(int buffer_size, const char *buffer, void *callback_data)
{
    get_bucket_common_data *version_data = (get_bucket_common_data *)callback_data;

    return simplexml_add(&(version_data->simpleXml), buffer, buffer_size);
}

void get_bucket_version_complete_callback(obs_status status,
    const obs_error_details *error_details,
    void *callback_data)
{
    COMMLOG(OBS_LOGINFO, "Enter %s successfully !", __FUNCTION__);

    get_bucket_common_data *version_data = (get_bucket_common_data *)callback_data;

    int ret = snprintf_s(version_data->common_data_return, sizeof(version_data->common_data),
        version_data->common_data_return_size, "%s", version_data->common_data);
    CheckAndLogNeg(ret, "snprintf_s", __FUNCTION__, __LINE__);

    (void)(*(version_data->responseCompleteCallback))
        (status, error_details, version_data->callback_data);

    simplexml_deinitialize(&(version_data->simpleXml));

    free(version_data);
    version_data = NULL;
    COMMLOG(OBS_LOGINFO, "Leave %s successfully !", __FUNCTION__);
}


void get_bucket_version_configuration(const obs_options *options, int status_return_size,
    char *status_return, obs_response_handler *handler, void *callback_data)
{
    request_params params;
    obs_use_api use_api = OBS_USE_API_S3;
    set_use_api_switch(options, &use_api);
    COMMLOG(OBS_LOGINFO, "get bucket version start!");
    get_bucket_common_data *version_data = (get_bucket_common_data*)malloc(sizeof(get_bucket_common_data));
    if (!version_data)
    {
        (void)(*(handler->complete_callback))(OBS_STATUS_OutOfMemory, 0, 0);
        COMMLOG(OBS_LOGERROR, "malloc version data failed !");
        return;
    }
    memset_s(version_data, sizeof(get_bucket_common_data), 0, sizeof(get_bucket_common_data));

    simplexml_initialize(&(version_data->simpleXml), &get_bucket_version_xml_callback, version_data);

    version_data->responsePropertiesCallback = handler->properties_callback;
    version_data->responseCompleteCallback = handler->complete_callback;
    version_data->callback_data = callback_data;
    version_data->common_data_return = status_return;
    version_data->common_data_return_size = status_return_size;
    string_buffer_initialize(version_data->common_data);

    memset_s(&params, sizeof(request_params), 0, sizeof(request_params));
    errno_t err = EOK;
    err = memcpy_s(&params.bucketContext, sizeof(obs_bucket_context), &options->bucket_options,
        sizeof(obs_bucket_context));
    CheckAndLogNoneZero(err, "memcpy_s", __FUNCTION__, __LINE__);
    err = memcpy_s(&params.request_option, sizeof(obs_http_request_option), &options->request_options,
        sizeof(obs_http_request_option));
    CheckAndLogNoneZero(err, "memcpy_s", __FUNCTION__, __LINE__);

    params.httpRequestType = http_request_type_get;
    params.properties_callback = &get_bucket_version_properties_callback;
    params.fromObsCallback = &get_bucket_version_data_callback;
    params.complete_callback = &get_bucket_version_complete_callback;
    params.callback_data = version_data;
    params.isCheckCA = is_check_ca(options);
    params.storageClassFormat = no_need_storage_class;
    params.subResource = "versioning";
    params.temp_auth = options->temp_auth;
    params.use_api = use_api;
    request_perform(&params);
    COMMLOG(OBS_LOGINFO, "get bucket version finish!");
}
